package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.dromara.system.common.UploadFiles;
import org.dromara.system.domain.BusinessFileRel;
import org.dromara.system.service.BusinessFileRelService;
import org.dromara.system.mapper.BusinessFileRelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author chs
* @description 针对表【business_file_rel(业务附件表)】的数据库操作Service实现
* @createDate 2025-02-10 15:30:49
*/
@Service
public class BusinessFileRelServiceImpl extends ServiceImpl<BusinessFileRelMapper, BusinessFileRel>
    implements BusinessFileRelService{

    @Resource
    private BusinessFileRelMapper businessFileRelMapper;

    /**
     * 通过业务表id和业务标识获取数据
     * @param: null
     * @return:
     * @author CHS
     */
    @Override
    public List<BusinessFileRel> fileMesSelect(Long businessId) {
        LambdaUpdateWrapper<BusinessFileRel> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(BusinessFileRel::getBusinessId,businessId)
            .eq(BusinessFileRel::getBusinessTable,"class_change")
            .eq(BusinessFileRel::getDelFlag,0);
        return businessFileRelMapper.selectList(wrapper);
    }

    /**
     *
     * @param businessId 业务id
     * @param businessTable 业务标识
     * @param files 文件集合
     * @return
     */
    @Override
    public Boolean fileMesSave(Long businessId, String businessTable, List<UploadFiles> files) {
        if (null == files ||files.isEmpty()){
            return true;
        }
        List<BusinessFileRel> uploadFiles = new ArrayList<>();
        files.forEach(e ->{
            BusinessFileRel businessFileRel = new BusinessFileRel();
            businessFileRel.setBusinessId(businessId);
            businessFileRel.setFileName(e.getFileName());
            businessFileRel.setBusinessTable(businessTable);
            businessFileRel.setFileUrl(e.getFileUrl());
            businessFileRel.setFileId(e.getId());
            uploadFiles.add(businessFileRel);
        });
        return saveBatch(uploadFiles);
    }

    /**
     * 根据业务表id删除附件
     * @param businessIds 业务表id
     * @param businessTable 业务表标识
     * @return
     */
    @Override
    public Boolean fileMesDelete(List<Long> businessIds, String businessTable) {
        LambdaQueryWrapper<BusinessFileRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(BusinessFileRel::getBusinessId,businessIds)
            .eq(BusinessFileRel::getBusinessTable,businessTable);
        return remove(wrapper);
    }

    @Override
    public Boolean fileMesDelete(Long businessId, String businessTable) {
        LambdaQueryWrapper<BusinessFileRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessFileRel::getBusinessId,businessId)
            .eq(BusinessFileRel::getBusinessTable,businessTable);
        return remove(wrapper);
    }

    /**
     * 删除指定业务id的多个附件
     * @param: businessId 业务id
     * @param: businessTable 业务标识
     * @param: fileIds 附件id
     * @author CHS
     * @date: 2024/7/25 14:08
     */
    @Override
    public Boolean fileMesDeleteByBusinessIdAndFileIds(Long businessId, String businessTable, List<Long> fileIds) {
        LambdaQueryWrapper<BusinessFileRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessFileRel::getBusinessId,businessId)
            .eq(BusinessFileRel::getBusinessTable,businessTable)
            .in(BusinessFileRel::getFileId,fileIds);
        return remove(wrapper);
    }

    /**
     * 获取附件
     * @param businessId
     * @param businessTable
     * @return
     */
    @Override
    public List<UploadFiles> fileMesList(Long businessId, String businessTable) {
        LambdaQueryWrapper<BusinessFileRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BusinessFileRel::getBusinessId,businessId)
            .eq(BusinessFileRel::getBusinessTable,businessTable);
        List<BusinessFileRel> list = list(wrapper);
        List<UploadFiles> files = new ArrayList<>();
        list.forEach(e->{
            UploadFiles uploadFiles = new UploadFiles();
            uploadFiles.setId(e.getFileId());
            uploadFiles.setFileName(e.getFileName());
            uploadFiles.setFileUrl(e.getFileUrl());
            files.add(uploadFiles);
        });
        return files;
    }

    /**
     *
     * @param businessIds 业务表id集合
     * @param businessTable 业务表标识
     * @return
     */
    @Override
    public Map<Long,List<UploadFiles>> fileMesListByIds(List<Long> businessIds, String businessTable) {
        Map<Long, List<UploadFiles>> resMap = new HashMap<>();
        if (null == businessIds ||businessIds.isEmpty()){
            return resMap;
        }
        LambdaQueryWrapper<BusinessFileRel> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(BusinessFileRel::getBusinessId,businessIds)
            .eq(BusinessFileRel::getBusinessTable,businessTable);
        List<BusinessFileRel> list = list(wrapper);
        Map<Long, List<BusinessFileRel>> collect = list.stream().collect(Collectors.groupingBy(BusinessFileRel::getBusinessId));

        collect.forEach((k,v) ->{
            ArrayList<UploadFiles> files = new ArrayList<>();
            v.forEach(e->{
                UploadFiles uploadFiles = new UploadFiles();
                uploadFiles.setId(e.getFileId());
                uploadFiles.setFileName(e.getFileName());
                uploadFiles.setFileUrl(e.getFileUrl());
                files.add(uploadFiles);
            });
            resMap.put(k,files);
        });
        return resMap;
    }

}




